package com.wwwzh.himalayafm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.live_adapter.BroadPageAdapter;
import com.wwwzh.himalayafm.entity.entity_broadtwo.ProEntity;
import com.wwwzh.himalayafm.entity.entity_broadtwo.ProResult;
import com.wwwzh.himalayafm.retrofit.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LiveTwoActivity extends AppCompatActivity implements Callback<ProEntity>, View.OnClickListener {
    private TextView tv;
    private List<ProResult> list;
    private ViewPager pager;
    private TabLayout tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_two);
        tv = (TextView) findViewById(R.id.live_two_tv);
        Button button = (Button) findViewById(R.id.finish_live_btn);
        button.setOnClickListener(this);
        Intent intent = getIntent();
        String text = intent.getStringExtra("text");
        tv.setText(text);
        pager = (ViewPager) findViewById(R.id.live_two_page);
        list = new ArrayList<>();
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getResult().enqueue(this);
        tab = (TabLayout) findViewById(R.id.live_two_tab);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }


    @Override
    public void onResponse(Call<ProEntity> call, Response<ProEntity> response) {
        ProEntity body = response.body();
        Log.d("fffff", "onResponse: "+body.getResult().get(0).getProvinceName());
        list=body.getResult();
        BroadPageAdapter adapter = new BroadPageAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);
        tab.setupWithViewPager(pager);
    }

    @Override
    public void onFailure(Call<ProEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
